//点击删除按钮
function removecart() {
    var remove_value = [];
    $('input[name="choose"]:checked').each(function () {
        //alert($(this).val());
        remove_value.push($(this).val())
    });
    if (remove_value.length == 0) {
        alert("请选择删除项")
    } else {
        //发送ajax请求
        $.ajax({
            url: "/cart/remove",
            traditional: true,
            data: {
                removeid: remove_value
            },
            success: function (data) {
                for (var i = 0; i < remove_value.length; i++) {
                    var index = remove_value[i]
                    $("#" + index).fadeOut('slow', function () {
                        $("#" + index).remove();
                    });
                }
                $("#allprice").text(0)
                $("#total_price").text(0)
                allprice = 0

            }

        })
    }


}


$(function () {

    allprice = parseFloat(0)
    var order_num;
    var chk_value = [];
    $("#createOrder").click(function () {

        $('input[name="choose"]:checked').each(function () {
            //alert($(this).val());
            chk_value.push($(this).val())
        });
        if ($("#allprice").text() == 0) {
            $("#createOrder").attr("data-target", "")
            alert("请选择商品")
        } else if ($("#pay_way option:selected").val() == '6') {
            $("#createOrder").attr("data-target", "")
            alert("请选择支付方式")
        } else {
            $.ajax({
                url: "/order/creat",
                traditional: true,
                data: {
                    pay_way: $("#pay_way option:selected").val(),	//支付方式
                    leave_message_context: $("#leave_message_context").val(),	//留言信息
                    total_price: $("#total_price").text(),
                    check: chk_value
                },
                success: function (data) {
                    if (data.indexOf('重新选择') != -1) {
                        $("#myModal").hide()
                        alert(data)
                        location.reload();
                    } else {
                        $("#createOrder").attr("data-target", "#myModal")
                        if ($("#pay_way option:selected").val() == 0) {
                            $("#sureorderprice").text(($("#total_price").text() * 0.95).toFixed(2))
                        } else {
                            $("#sureorderprice").text($("#total_price").text())
                        }
                        if (pay_way == 0) {	//线上支付才出现倒计时
                            $("#contents").text("请在15分钟内确认订单并完成在线支付,否则将自动取消该订单")
                            $('#demo').pietimer({
                                seconds: 600,
                                color: 'rgba(0, 0, 0, 0.8)',
                                height: 100,
                                width: 100
                            }, function () {
                                alert("时间到，失效了")
                                location.reload()
                            });
                            $('#demo').pietimer('start');
                            return false;
                        } else {
                            if (pay_way == 1) {
                                $("#contents").text("付款方式：线下付款")
                            } else {
                                $("#contents").text("付款方式：钱包余额支付")
                            }
                        }
                    }
                }
            })
        }


    })

    $(document).on("click", "tr", function () {//修改成这样的写法
        //根据选中的东西确认价格
        var v = $(this).find("input").prop("checked")
        if (v == true) {
            $(this).removeClass('mycsss')
            $(this).find("input").prop("checked", false)
            var price = $(this).find("h4").text()
            var price1 = parseFloat(price)
            allprice = allprice - price1;
            var value = Math.abs(allprice.toFixed(2))
            $("#allprice").text(value)
            var discount = $("#discount").val()	//获取到折扣
            var vprice = Math.abs((allprice * discount).toFixed(2))

            $("#total_price").text(vprice)

        }
        if (v == false) {
            $(this).addClass('mycsss')
            $(this).find("input").prop("checked", true)

            var price = $(this).find("h4").text()
            var price1 = parseFloat(price)
            allprice = allprice + price1;
            var value = Math.abs(allprice.toFixed(2))
            $("#allprice").text(value)
            var discount = $("#discount").val()	//获取到折扣
            var vprice = Math.abs((allprice * discount).toFixed(2))
            $("#total_price").text(vprice)

        }
    });
    //点击确认订单
    $("#payButton").click(function () {
        var pay_way = $("#pay_way option:selected").val();	//选中的支付方式
        if (pay_way == 1) {
            //线下支付
            alert("预定成功，请在线下入住时候付款")
            //移除购物车中结算的商品
            $.ajax({
                url: "/order/cartdelete",
                traditional: true,
                data: {
                    check: chk_value
                },
                success: function (data) {
                    location.reload()
                }
            })

        } else if (pay_way == 0) {
            //线上支付，跳转支付宝
            alert("预定成功，跳转支付宝咯" + order_num)
            location.href = "/pay/pay?out_trade_no=" + order_num + "&total_amount=" + $("#sureorderprice").text() + "&check=" + chk_value
        } else if (pay_way == 2) {
            //alert("扣钱包里面的钱")
            var pwd = prompt("请输入您的支付密码：")
            if (pwd == null) {
                alert("您取消了输入密码")
            } else {
                $.ajax({
                    url: "/order/paywallet",
                    data: {
                        total_price: $("#sureorderprice").text(),
                        user_password: pwd,
                        order_num: order_num
                    },
                    success: function (data) {
                        alert(data)
                        if (data == '支付成功') {
                            //移除购物车中结算的商品
                            $.ajax({
                                url: "/order/cartdelete",
                                traditional: true,
                                data: {
                                    check: chk_value
                                },
                                success: function (data) {
                                    location.reload();
                                }
                            })
                        } else {
                            location.reload()
                        }
                    }
                })
            }
            //location.reload()
        }
    })
    //点击取消按钮
    $("#cancelButton").click(function () {
        alert("发送请求，取消订单")
        $.ajax({
            url: "/order/changeOrderState",
            data: {
                order_num: order_num
            },
            success: function (data) {
                alert(data)
                location.reload()
            }
        })
    })
})
